// 检查项类型枚举
export enum ComplianceType {
  RISK = 'risk',
  GUARANTEE = 'guarantee'
}

// 风险等级枚举
export enum RiskLevel {
  VERY_HIGH = 'very-high', // 电信涉诈风险评估
  HIGH = 'high',
  MEDIUM = 'medium',
  LOW = 'low',
  VERY_LOW = 'very-low', // 电信涉诈风险评估
  NOT_APPLICABLE = 'not-applicable'
}

// 合规状态枚举
export enum ComplianceStatus {
  NON_COMPLIANT = 'non-compliant',
  PARTIALLY_COMPLIANT = 'partially-compliant',
  FULLY_COMPLIANT = 'fully-compliant',
  NOT_APPLICABLE = 'not-applicable'
}

// 进度状态枚举
export enum ProgressStatus {
  IN_PROGRESS = 'in-progress',
  REMEDIATION = 'remediation',
  COMPLETED = 'completed'
}

// 视图模式枚举
export enum ViewMode {
  MATRIX = 'matrix',
  LIST = 'list',
  GRID = 'grid'
}

// 风险项筛选标签枚举
export enum RiskFilterTab {
  ALL = 'all',
  HIGH = 'high',
  MEDIUM = 'medium',
  LOW = 'low',
  UNASSOCIATED = 'unassociated'
}

// 附件类型
export interface Attachment {
  id: string;
  name: string;
  type: string;
  size: number;
}

// 成熟度等级统计
export interface MaturityLevelStat {
  level: number;
  total: number;
  completed: number;
  progress: number;
}

// 检查项接口
export interface ComplianceItem {
  id: string;
  plan_id: string;
  title: string; // 标题
  description: string; // 描述
  guide: string; // 指引
  inspector: string; // 检查描述
  compliance_type: ComplianceType; // 检查项类型
  risk_level?: RiskLevel;  // 仅风险项有此属性
  compliance_status: ComplianceStatus; // 合规状态
  progress_status: ProgressStatus; // 进度状态
  attachments: Attachment[]; // 附件
  source_type: string; // 来源类型
  related_guarantees?: string[];  // 仅风险项可关联保障项
  updated_at?: string;  // 最后更新时间
  maturity_level?: number; // 成熟度等级（1-5，用于数据安全评估）
}

// 风险-保障关联接口
export interface RiskAssociationItem {
  risk_id: string;
  guarantee_ids: string[];
}

// 风险项关联统计
export interface RiskAssociationStats {
  totalRisks: number;
  associatedRisks: number;
  totalAssociations: number;
}

// 风险项保障覆盖率
export interface RiskCoverage {
  risk_id: string;
  coverage_rate: number; // 0-100
  coverage_level: 'high' | 'medium' | 'low' | 'none'; // 根据覆盖率计算的级别
}

// 风险关联管理筛选器
export interface RiskAssociationFilters {
  tab: RiskFilterTab;
  searchTerm: string;
  viewMode: ViewMode;
}

// 过滤器接口
export interface FiltersType {
  compliance_type: string;
  riskLevel: string;
  progressStatus: string;
  /**
   * 保障状态筛选
   */
  complianceStatus: string;
  /**
   * 根据检查项标题进行搜索过滤
   */
  searchTerm: string;
  /**
   * 成熟度等级筛选（1-5，用于数据安全评估）
   */
  maturityLevel: string;
}

export interface MaturityLevelStat {
  level: number
  total: number
  completed: number
  progress: number
}

// ==================== 标签文本配置 ====================

// 风险等级标签配置 - 根据不同评估类型显示不同的选项和文本
export const RISK_LEVEL_CONFIG: Record<string, Array<{ value: RiskLevel; label: string }>> = {
  '101': [ // 双新评估
    { value: RiskLevel.HIGH, label: '高风险' },
    { value: RiskLevel.LOW, label: '低风险' },
    { value: RiskLevel.NOT_APPLICABLE, label: '不适用' }
  ],
  '102': [ // 电信涉诈风险评估
    { value: RiskLevel.VERY_HIGH, label: '很高' },
    { value: RiskLevel.HIGH, label: '高' },
    { value: RiskLevel.MEDIUM, label: '中' },
    { value: RiskLevel.LOW, label: '低' },
    { value: RiskLevel.VERY_LOW, label: '很低' },
    { value: RiskLevel.NOT_APPLICABLE, label: '不适用' }
  ],
  default: [ // 默认配置
    { value: RiskLevel.HIGH, label: '高风险' },
    { value: RiskLevel.MEDIUM, label: '中风险' },
    { value: RiskLevel.LOW, label: '低风险' },
    { value: RiskLevel.NOT_APPLICABLE, label: '不适用' }
  ]
}

// 合规状态标签配置 - 根据不同评估类型显示不同的选项和文本
export const COMPLIANCE_STATUS_CONFIG: Record<string, Array<{ value: ComplianceStatus; label: string }>> = {
  '101': [ // 双新评估
    { value: ComplianceStatus.FULLY_COMPLIANT, label: '满足' },
    { value: ComplianceStatus.NON_COMPLIANT, label: '不满足' },
    { value: ComplianceStatus.NOT_APPLICABLE, label: '不适用' }
  ],
  '102': [ // 电信涉诈风险评估
    { value: ComplianceStatus.FULLY_COMPLIANT, label: '符合' },
    { value: ComplianceStatus.PARTIALLY_COMPLIANT, label: '部分符合' },
    { value: ComplianceStatus.NON_COMPLIANT, label: '不符合' },
    { value: ComplianceStatus.NOT_APPLICABLE, label: '不适用' }
  ],
  default: [ // 默认配置
    { value: ComplianceStatus.FULLY_COMPLIANT, label: '完全满足' },
    { value: ComplianceStatus.PARTIALLY_COMPLIANT, label: '部分满足' },
    { value: ComplianceStatus.NON_COMPLIANT, label: '不满足' },
    { value: ComplianceStatus.NOT_APPLICABLE, label: '不适用' }
  ]
}

// ==================== 辅助函数 ====================

/**
 * 获取风险等级选项（用于表单下拉框）
 * @param assessmentType 评估类型
 * @returns 风险等级选项数组
 */
export const getRiskLevelOptions = (assessmentType?: string) => {
  return RISK_LEVEL_CONFIG[assessmentType || 'default'] || RISK_LEVEL_CONFIG.default
}

/**
 * 获取合规状态选项（用于表单下拉框）
 * @param assessmentType 评估类型
 * @returns 合规状态选项数组
 */
export const getComplianceStatusOptions = (assessmentType?: string) => {
  return COMPLIANCE_STATUS_CONFIG[assessmentType || 'default'] || COMPLIANCE_STATUS_CONFIG.default
}

/**
 * 获取风险等级的中文文本
 * @param riskLevel 风险等级枚举值
 * @param assessmentType 评估类型
 * @returns 风险等级的中文描述
 */
export const getRiskLevelText = (riskLevel: RiskLevel, assessmentType?: string): string => {
  const config = getRiskLevelOptions(assessmentType)
  const item = config.find(c => c.value === riskLevel)
  return item?.label || '未知'
}

/**
 * 获取合规状态的中文文本
 * @param status 合规状态枚举值
 * @param assessmentType 评估类型
 * @returns 合规状态的中文描述
 */
export const getComplianceStatusText = (status: ComplianceStatus, assessmentType?: string): string => {
  const config = getComplianceStatusOptions(assessmentType)
  const item = config.find(c => c.value === status)
  return item?.label || '未知状态'
}

/**
 * 获取风险等级文本映射（用于导出等场景）
 * @param assessmentType 评估类型
 * @returns 风险等级值到中文文本的映射对象
 */
export const getRiskLevelMap = (assessmentType?: string): Record<string, string> => {
  const config = getRiskLevelOptions(assessmentType)
  return config.reduce((acc, item) => {
    acc[item.value] = item.label
    return acc
  }, {} as Record<string, string>)
}

/**
 * 获取合规状态文本映射（用于导出等场景）
 * @param assessmentType 评估类型
 * @returns 合规状态值到中文文本的映射对象
 */
export const getComplianceStatusMap = (assessmentType?: string): Record<string, string> => {
  const config = getComplianceStatusOptions(assessmentType)
  return config.reduce((acc, item) => {
    acc[item.value] = item.label
    return acc
  }, {} as Record<string, string>)
}

